#!/bin/bash
# KWDB SDK 环境诊断脚本

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}======================================"
echo "  KWDB SDK 环境诊断工具"
echo "======================================${NC}"

# 1. 系统信息
echo ""
echo -e "${BLUE}========== 1. 系统信息 ==========${NC}"
echo "操作系统: $(uname -s)"
echo "架构:     $(uname -m)"
if [ -f /etc/os-release ]; then
    source /etc/os-release
    echo "发行版:   $PRETTY_NAME"
fi
echo "内核版本: $(uname -r)"
echo "主机名:   $(hostname)"

# 2. Java 环境
echo ""
echo -e "${BLUE}========== 2. Java 环境 ==========${NC}"
if command -v java &> /dev/null; then
    echo -e "${GREEN}✓ Java 已安装${NC}"
    java -version 2>&1 | sed 's/^/  /'
    echo "  Java 路径: $(which java)"
    echo "  JAVA_HOME: ${JAVA_HOME:-未设置}"
else
    echo -e "${RED}✗ Java 未安装${NC}"
    echo "  安装命令: sudo apt-get install -y openjdk-11-jdk"
fi

# 3. Maven 环境
echo ""
echo -e "${BLUE}========== 3. Maven 环境 ==========${NC}"
if command -v mvn &> /dev/null; then
    echo -e "${GREEN}✓ Maven 已安装${NC}"
    mvn -version 2>&1 | head -n 3 | sed 's/^/  /'
    echo "  Maven 路径: $(which mvn)"
    
    # 检查 Maven 配置
    if [ -f ~/.m2/settings.xml ]; then
        echo -e "  ${GREEN}✓ Maven 配置文件存在${NC} (~/.m2/settings.xml)"
    else
        echo -e "  ${YELLOW}⚠ Maven 配置文件不存在${NC}"
    fi
else
    echo -e "${RED}✗ Maven 未安装${NC}"
    echo "  安装命令: sudo apt-get install -y maven"
fi

# 4. Protobuf 环境
echo ""
echo -e "${BLUE}========== 4. Protobuf 环境 ==========${NC}"
if command -v protoc &> /dev/null; then
    echo -e "${GREEN}✓ protoc 已安装${NC}"
    PROTOC_VERSION=$(protoc --version | awk '{print $2}')
    echo "  版本: $PROTOC_VERSION"
    echo "  路径: $(which protoc)"
    
    # 检查版本是否足够新
    MAJOR=$(echo $PROTOC_VERSION | cut -d. -f1)
    MINOR=$(echo $PROTOC_VERSION | cut -d. -f2)
    if [ "$MAJOR" -ge 3 ] && [ "$MINOR" -ge 20 ]; then
        echo -e "  ${GREEN}✓ 版本满足要求 (>= 3.20.0)${NC}"
    else
        echo -e "  ${YELLOW}⚠ 版本过低，推荐 >= 3.20.0${NC}"
    fi
else
    echo -e "${RED}✗ protoc 未安装 (重要!)${NC}"
    echo "  这会导致 Java SDK 编译失败"
    echo "  安装命令: sudo apt-get install -y protobuf-compiler"
fi

# 5. 构建工具
echo ""
echo -e "${BLUE}========== 5. 构建工具 ==========${NC}"

echo -n "CMake:     "
if command -v cmake &> /dev/null; then
    CMAKE_VERSION=$(cmake --version | head -n 1 | awk '{print $3}')
    echo -e "${GREEN}✓ $CMAKE_VERSION${NC}"
else
    echo -e "${YELLOW}⚠ 未安装 (C++ SDK 需要)${NC}"
fi

echo -n "Make:      "
if command -v make &> /dev/null; then
    MAKE_VERSION=$(make --version | head -n 1)
    echo -e "${GREEN}✓ $MAKE_VERSION${NC}"
else
    echo -e "${YELLOW}⚠ 未安装${NC}"
fi

echo -n "GCC:       "
if command -v gcc &> /dev/null; then
    GCC_VERSION=$(gcc --version | head -n 1)
    echo -e "${GREEN}✓ $GCC_VERSION${NC}"
else
    echo -e "${YELLOW}⚠ 未安装${NC}"
fi

echo -n "G++:       "
if command -v g++ &> /dev/null; then
    GPP_VERSION=$(g++ --version | head -n 1)
    echo -e "${GREEN}✓ $GPP_VERSION${NC}"
else
    echo -e "${YELLOW}⚠ 未安装${NC}"
fi

# 6. 项目目录检查
echo ""
echo -e "${BLUE}========== 6. 项目目录 ==========${NC}"
PROJECT_DIR="/home/kvgroup/chaomei/kwdb_jx/KWDB-sdk"

if [ -d "$PROJECT_DIR" ]; then
    echo -e "${GREEN}✓ 项目目录存在${NC}"
    echo "  路径: $PROJECT_DIR"
    
    # 检查子目录
    for dir in java-sdk server cpp-sdk proto docs; do
        if [ -d "$PROJECT_DIR/$dir" ]; then
            echo -e "  ${GREEN}✓${NC} $dir/"
        else
            echo -e "  ${RED}✗${NC} $dir/ (缺失)"
        fi
    done
else
    echo -e "${RED}✗ 项目目录不存在${NC}"
    echo "  期望路径: $PROJECT_DIR"
fi

# 7. 编译产物检查
echo ""
echo -e "${BLUE}========== 7. 编译产物 ==========${NC}"

# Java SDK
JAVA_SDK_JAR="$PROJECT_DIR/java-sdk/target/kwdb-sdk-1.0.0.jar"
if [ -f "$JAVA_SDK_JAR" ]; then
    SIZE=$(ls -lh "$JAVA_SDK_JAR" | awk '{print $5}')
    MTIME=$(stat -c %y "$JAVA_SDK_JAR" 2>/dev/null | cut -d'.' -f1)
    echo -e "${GREEN}✓ Java SDK JAR${NC}"
    echo "  文件: kwdb-sdk-1.0.0.jar"
    echo "  大小: $SIZE"
    echo "  时间: $MTIME"
else
    echo -e "${YELLOW}⚠ Java SDK 未编译${NC}"
fi

# 服务端
SERVER_JAR="$PROJECT_DIR/server/target/kwdb-server-1.0.0.jar"
if [ -f "$SERVER_JAR" ]; then
    SIZE=$(ls -lh "$SERVER_JAR" | awk '{print $5}')
    MTIME=$(stat -c %y "$SERVER_JAR" 2>/dev/null | cut -d'.' -f1)
    echo -e "${GREEN}✓ 服务端 JAR${NC}"
    echo "  文件: kwdb-server-1.0.0.jar"
    echo "  大小: $SIZE"
    echo "  时间: $MTIME"
else
    echo -e "${YELLOW}⚠ 服务端未编译${NC}"
fi

# 8. 网络连接
echo ""
echo -e "${BLUE}========== 8. 网络连接 ==========${NC}"

echo -n "Maven 中央仓库: "
if ping -c 1 -W 2 repo.maven.apache.org &> /dev/null; then
    echo -e "${GREEN}✓ 可达${NC}"
else
    echo -e "${RED}✗ 不可达${NC}"
fi

echo -n "Aliyun Maven:   "
if ping -c 1 -W 2 maven.aliyun.com &> /dev/null; then
    echo -e "${GREEN}✓ 可达${NC}"
else
    echo -e "${RED}✗ 不可达${NC}"
fi

echo -n "GitHub:         "
if ping -c 1 -W 2 github.com &> /dev/null; then
    echo -e "${GREEN}✓ 可达${NC}"
else
    echo -e "${YELLOW}⚠ 不可达 (不影响编译)${NC}"
fi

# 9. 系统资源
echo ""
echo -e "${BLUE}========== 9. 系统资源 ==========${NC}"

# 内存
TOTAL_MEM=$(free -h | grep Mem | awk '{print $2}')
AVAIL_MEM=$(free -h | grep Mem | awk '{print $7}')
echo "内存: $AVAIL_MEM 可用 / $TOTAL_MEM 总计"

# 磁盘
DISK_USAGE=$(df -h "$PROJECT_DIR" 2>/dev/null | tail -n 1 | awk '{print $5}')
DISK_AVAIL=$(df -h "$PROJECT_DIR" 2>/dev/null | tail -n 1 | awk '{print $4}')
echo "磁盘: $DISK_AVAIL 可用 (使用率: $DISK_USAGE)"

# CPU
CPU_COUNT=$(nproc)
echo "CPU:  $CPU_COUNT 核心"

# 10. 服务状态
echo ""
echo -e "${BLUE}========== 10. 服务状态 ==========${NC}"

if command -v netstat &> /dev/null; then
    if netstat -tlnp 2>/dev/null | grep -q ":9090"; then
        echo -e "${GREEN}✓ KWDB 服务器正在运行 (端口 9090)${NC}"
    else
        echo -e "${YELLOW}⚠ KWDB 服务器未运行${NC}"
    fi
elif command -v ss &> /dev/null; then
    if ss -tlnp 2>/dev/null | grep -q ":9090"; then
        echo -e "${GREEN}✓ KWDB 服务器正在运行 (端口 9090)${NC}"
    else
        echo -e "${YELLOW}⚠ KWDB 服务器未运行${NC}"
    fi
else
    echo -e "${YELLOW}⚠ 无法检查服务状态 (netstat/ss 未安装)${NC}"
fi

# 总结和建议
echo ""
echo -e "${BLUE}======================================"
echo "  诊断完成"
echo "======================================${NC}"

# 检查关键问题
ISSUES=0

if ! command -v java &> /dev/null; then
    echo -e "${RED}[关键] Java 未安装${NC}"
    ISSUES=$((ISSUES+1))
fi

if ! command -v mvn &> /dev/null; then
    echo -e "${RED}[关键] Maven 未安装${NC}"
    ISSUES=$((ISSUES+1))
fi

if ! command -v protoc &> /dev/null; then
    echo -e "${RED}[关键] Protobuf 未安装 - 会导致编译失败${NC}"
    ISSUES=$((ISSUES+1))
fi

if [ ! -d "$PROJECT_DIR" ]; then
    echo -e "${RED}[关键] 项目目录不存在${NC}"
    ISSUES=$((ISSUES+1))
fi

if [ $ISSUES -eq 0 ]; then
    echo ""
    echo -e "${GREEN}✓ 环境检查通过！可以开始编译${NC}"
    echo ""
    echo "建议执行："
    echo "  ./build_all.sh      # 编译全部"
    echo "  ./quick_test.py     # 运行测试"
else
    echo ""
    echo -e "${YELLOW}发现 $ISSUES 个关键问题，请先解决${NC}"
    echo ""
    echo "快速修复："
    echo "  ./fix_protoc.sh     # 修复 protoc 问题"
    echo ""
    echo "或查看文档："
    echo "  Linux环境编译部署指南.md"
fi

echo ""

